package com.eqdd.yiqidian.utils;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;

import com.afollestad.materialdialogs.MaterialDialog;
import com.eqdd.yiqidian.ListItem110Custom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.BaseBean;
import com.eqdd.yiqidian.bean.ErweimaBean;
import com.eqdd.yiqidian.bean.rebuild.SignListBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.web.WebActivity;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.eqdd.yiqidian.widget.EditDialogFragment;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 18-2-2  下午1:57.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class ScanQRUtil {

    private final String HAOYOU = "2";
    private final String QUNZU = "1";
    private final String SIGN = "3";
    private MaterialDialog showDialog;
    private BaseActivity activity;

    public ScanQRUtil(BaseActivity activity) {
        this.activity = activity;
    }

    public void dealQR(String result, String address) {
        try {
            ErweimaBean<SignListBean> erweimaBean = new Gson().fromJson(result, new TypeToken<ErweimaBean<SignListBean>>() {
            }.getType());
            if (erweimaBean != null) {
                if (erweimaBean.getType().equals(SIGN)) {
                    trainSign(activity, erweimaBean.getData(), address);
                } else {
                    showInputDialog(erweimaBean);
                }
            } else {
                Bundle bundle = new Bundle();
                bundle.putString(Config.URL, result);
                bundle.putString(Config.TITLE, "扫描结果");
                activity.startActivity(WebActivity.class, bundle);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Bundle bundle = new Bundle();
            bundle.putString(Config.URL, result);
            bundle.putString(Config.TITLE, "扫描结果");
            activity.startActivity(WebActivity.class, bundle);
        }
    }
    private void trainSign(BaseActivity activity, SignListBean signListBean, String address) {
        activity.showLoading("正在签到...");
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.TRAIN_SIGN)
                .params("userGuid", AppConstent.user.getGuid())
                .params("userName", AppConstent.user.getUsername())
                .params("siInfoId", signListBean.getId())
                .params("depid", AppConstent.user.getDepartId())
                .params("depName", AppConstent.user.getDepartment())
                .params("postid", AppConstent.user.getPostId())
                .params("postName", AppConstent.user.getPost())
                .params("signInPosition", address)
                .params("macAddress", DakaUtil.getPhoneMac(activity))
                .params("wifiName", DakaUtil.getWiFiName(activity))
                .params("phoneModel", DakaUtil.getSystemModel())
                .params("uuid", DakaUtil.getPhoneUUID(activity))
                .execute(new JsonCallBack<HttpResult>() {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        activity.hideLoading(httpResult.getMsg());
                        if (httpResult.getStatus() == 200 || httpResult.getStatus() == 208) {
                            if (showDialog == null) {
                                ListItem110Custom inflate = DataBindingUtil.inflate(activity.getLayoutInflater(), R.layout.list_item_110, null, false);
                                inflate.tvAddress.setText("培训地址:" + signListBean.getTheplace());
                                inflate.tvLecture.setText("当前课程:" + signListBean.getTheTheme());
                                inflate.tvTeacher.setText("讲师:" + signListBean.getTeacherName());
                                inflate.tvTime.setText("培训时间:" + signListBean.getTheTrainTime());
                                inflate.tvTarget.setText("受训对象" + signListBean.getTrainees());
                                ClickUtil.click(inflate.tvSure, () -> showDialog.hide());
                                showDialog = new MaterialDialog.Builder(activity)
                                        .customView(inflate.getRoot(), false)
                                        .build();
                            }
                            showDialog.show();
                        }

                    }

                    @Override
                    public void onError(Response<HttpResult> response) {
                        super.onError(response);
                        activity.hideLoading(Config.SERVER_ERROR);
                    }
                });
    }

    private void showInputDialog(ErweimaBean erweimaBean) {
        HashMap<String, String> urlTypes = new HashMap<>();
        urlTypes.put(HAOYOU, HttpConfig.INVITE_FRIEND);
        urlTypes.put(QUNZU, HttpConfig.ADD_GROUP);
        String title = erweimaBean.getType().equals(HAOYOU) ? "添加" + erweimaBean.getName() + "为好友" : "加入" + erweimaBean.getName() + "群组";
        EditDialogFragment editDiglog = ChooseAlertView.getEditDiglog(title, "我是" + AppConstent.user.getUsername(), "请输入验证信息",
                null, null, contents -> OkGo.<HttpResult>post(HttpConfig.BASE_URL + urlTypes.get(erweimaBean.getType()))
                        .params("userGuid", ";" + AppConstent.user.getGuid())
                        .params("Groupid", erweimaBean.getUgid())
                        .params("GroupName", erweimaBean.getName())
                        .params("userid", AppConstent.user.getGuid())
                        .params("friendid", erweimaBean.getUgid())
                        .params("addcontent", contents.get(0))
                        .execute(new DialogCallBack<HttpResult>(activity) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    activity.finish();
                                }
                            }
                        }));
        FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
        ft.add(editDiglog, "123");
        ft.commitAllowingStateLoss();
    }
}
